/*
dialog 使用方法
Alert('请输入EMAIL!');
Alert({
		msg: '提交失败！',
		onOk: function(){
		}
});
Confirm({
		msg: 'Password为空，是否要继续？',
		onOk: function(){
				
		},
		onCancel: function(){

		}
});
*/
/*主题设置*/
$(".dropdown ul .alert").click(function(){
	var _theme_this = $(this);
	$("link[title='" + _theme_this.attr("data-theme") + "']").prop("disabled", false);
	setTimeout(function(){
		$("link[title='default']").prop("disabled", true);
		$("link[title='theme_1']").prop("disabled", true);
		setCookie('system_theme',_theme_this.attr("data-theme"),'d365');
		$("link[title='" + _theme_this.attr("data-theme") + "']").prop("disabled", false);
	},10);
});

$(function(){
  $("link[title='default']").prop("disabled", true);
  $("link[title='theme_1']").prop("disabled", true);
  var _theme_name = getCookie('system_theme');
  if ( nulls(_theme_name) == '-' ) {
    _theme_name = 'default';
  }
  $("link[title='" + _theme_name + "']").prop("disabled", false);
})

function nulls(val){
	return val==null?"-":val;
}

//图片放大
$(".blowUpImg").click(function(){
	top.$("body").find(".blowUpImgShow").remove();
  top.$("body").append('<img class="blowUpImgShow" title="点击关闭" src="' + $(this).attr("src") + '" style="position: fixed; z-index: 1200; max-width: 90%; max-height: 90%; left: 50%; top: 50%; -webkit-transform: translate3d(-50%,-50%,0); transform: translate3d(-50%,-50%,0);" onclick="this.remove()">');
});

var talertTimeOut = 0;
/*自定义位置提示文字*/
function talert(valid, msg,color){
  clearTimeout(talertTimeOut);
	if ( $(valid).length > 0 ) {
		$(".mytooltip").remove();
		t_top = $(valid).offset().top;
		flag = 'right';
		var t_left = $(valid).offset().left + $(valid).outerWidth() + 10;
		if ( (300 + t_left) > $("body").width() ) {
			t_top += $(valid).outerHeight() + 10;
			t_left = $(valid).offset().left;
			flag = 'bottom';
		}
		$("body").append('<div style="z-index: 1056;left: '+t_left+'px; top: '+t_top+'px;" class="mytooltip mytooltip_' + flag + ' mytooltip_' + color + '">'+msg+'</div>');
		talertTimeOut = setTimeout(function(){
			$(".mytooltip").remove();
		},2000);
		$(".mytooltip").click(function(){
			$(this).remove();
      clearTimeout(talertTimeOut);
		});
	}
}

/*时间戳转日期*/
function timeToDate(datetime,tmp){
	var time_tmp = new Date(parseInt(datetime)*1000);
	var y = time_tmp.getFullYear();
	var m = time_tmp.getMonth()+1;
	var d = time_tmp.getDate();
	var h = time_tmp.getHours();
	var i = time_tmp.getMinutes();
	var s = time_tmp.getSeconds();
	if ( tmp == 'Y-m-d' ) {
		return y+'-'+timeAddZeor(m)+'-'+timeAddZeor(d);
	} else if ( tmp == 'Y-m' ) {
		return y+'-'+timeAddZeor(m);
	} else {
		return y+'-'+timeAddZeor(m)+'-'+timeAddZeor(d)+' '+timeAddZeor(h)+':'+timeAddZeor(i)+':'+timeAddZeor(s);
	}
}

function timeAddZeor(m){
	return m<10?'0'+m:m;
}

/*后退按钮*/
$(".btn-back").click(function(){
	history.back();
});

/*关闭按钮*/
$(".btn-back-iframe").click(function(){
	closeIframe();
});

function closeIframe(){
	parent.$(".mdthickbox-modal").modal("hide");
}

function loading(){
	str = "<div id='ajaxload' style='position: fixed; left: 0; top: 0; z-index: 88; background: rgba(255,255,255,0.5); width: 100%; height: 100%;'><img src='/Public/images/loading.gif' style='width: 100px; height: 20px; position: absolute; top: 50%; left: 50%; margin-top: -10px; margin-left: -50px;'></div>";
	$("body").append(str);
}

function noloading(){
	$("#ajaxload").remove();
}

/*检测字符串是否包含特殊字符*/
function checkStr(str){
	szReg = /[\',:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/;
	var bChk = szReg.test(str);
	if (!bChk) {
		return true;
	}
}

/*检测是否整数*/
function checkAllNum(num){
	szReg = /^-?[0-9]\d*$/;//整数
	var bChk = szReg.test(num);
	if (!bChk) {
		return true;
	}
}

/*检测是否正整数 含0*/
function checkNum(num){
	szReg = /^\d+(\d+)?$/;//正整数 含0
	var bChk = szReg.test(num);
	if (!bChk) {
		return true;
	}
}

/*检测是否正整数 不含0*/
function checkNums(num){
	szReg = /^[0-9]*[1-9][0-9]*$/;//正整数 不含0
	var bChk = szReg.test(num);
	if (!bChk) {
		return true;
	}
}

/*检测是否数值为金额*/
function checkMoney(money){
	szReg = /^\d+(\.\d{1,2})?$/;
	var bChk = szReg.test(money);
	if (!bChk) {
		return true;
	}
}

/*检测字符串是否邮箱*/
function checkEmail(email){
	szReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var bChk = szReg.test(email);
	if (!bChk) {
		return true;
	}
}

/*检测手机号*/
function checkPhone(phone){
	szReg = /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8]))\d{8}$/;
	var bChk = szReg.test(phone);
	if (!bChk) {
		return true;
	}
}

/*检测身份证号码*/
function checkIdcard(Idcard){
	szReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	var bChk = szReg.test(Idcard);
	if (!bChk) {
		return true;
	}
}

/*检测银行卡号*/
function checkBank(Bank){
	szReg = /^(\d{16}|\d{19})$/;
	var bChk = szReg.test(Bank);
	if (!bChk) {
		return true;
	}
}

/*是否确认删除*/
function tableDel(url){
	Confirm({
		msg : "是否确认删除？",
		onOk : function(){
			location.href = url;
		}
	});
}

function ajax(url, param, datat, type){
  loading();
  return $.ajax({
    url: url,
    data: param || {},
    type: type || 'post',
    dataType: datat || 'json',
    timeout: '300000',
    error:function(XMLHttpRequest, textStatus, errorThrown){
      //alert(JSON.stringify(msg));
      // if (true) {
      //   Alert(XMLHttpRequest.responseText);
      // } else {
        setTimeout(function(){
          //top.Alert('系统繁忙，请稍后再试！');
        },200);
      // }
    },
    complete: function(XHR,TS){
      noloading();
      XHR=null;
    }
  });
}

/*清除图片*/
function clearImage(valid,event){
  event.stopPropagation();
  if ( $("#"+valid+"Url").attr("src") == "/Public/images/setimg/image.png" ) {
    return false;
  }
  Confirm({
    msg : '是否确认删除图片？',
    onOk : function(){
      $("#"+valid).val("");
      $("#"+valid+"Url").attr("src","/Public/images/setimg/image.png");
    }
  });
}

/*显示清楚图片*/
$(".thumbnail-box img").mouseenter(function(){
  if ( $(this).attr("src") != '/Public/images/setimg/image.png' ) {
    $(this).parent().find("span").css("display", "block");
  }
});

$(".thumbnail-box .thumbnail-clear").mouseleave(function(){
  if ( $(this).css("display") == 'block' ) {
    $(this).css("display", "none");
  }
});
